package chpt10.texteditor;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class EditorFrame extends JFrame {
    private JTextArea textArea;

    public EditorFrame() {
        setTitle("文本编辑器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 400);
        setLocationRelativeTo(null);

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();

        // 创建文件菜单
        JMenu fileMenu = new JMenu("文件");
        fileMenu.setMnemonic(KeyEvent.VK_F);

        // 创建打开菜单项
        JMenuItem openItem = new JMenuItem("打开");
        openItem.setMnemonic(KeyEvent.VK_O);
        openItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打开文件的操作
                // ...
            }
        });

        // 创建保存菜单项
        JMenuItem saveItem = new JMenuItem("保存");
        saveItem.setMnemonic(KeyEvent.VK_S);
        saveItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 保存文件的操作
                // ...
            }
        });

        // 创建退出菜单项
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // 添加菜单项到文件菜单
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        // 创建编辑菜单
        JMenu editMenu = new JMenu("编辑");
        editMenu.setMnemonic(KeyEvent.VK_E);

        // 创建复制菜单项
        JMenuItem copyItem = new JMenuItem("复制");
        copyItem.setMnemonic(KeyEvent.VK_C);
        copyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.copy();
            }
        });

        // 创建粘贴菜单项
        JMenuItem pasteItem = new JMenuItem("粘贴");
        pasteItem.setMnemonic(KeyEvent.VK_P);
        pasteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.paste();
            }
        });

        // 添加菜单项到编辑菜单
        editMenu.add(copyItem);
        editMenu.add(pasteItem);

        // 创建关于菜单
        JMenu aboutMenu = new JMenu("关于");
        aboutMenu.setMnemonic(KeyEvent.VK_A);

        // 添加菜单到菜单栏
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(aboutMenu);

        // 设置菜单栏
        setJMenuBar(menuBar);

        // 创建文本区域
        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane, BorderLayout.CENTER);

        // 创建右键菜单
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem copyMenuItem = new JMenuItem("复制");
        copyMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.copy();
            }
        });
        JMenuItem pasteMenuItem = new JMenuItem("粘贴");
        pasteMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.paste();
            }
        });
        popupMenu.add(copyMenuItem);
        popupMenu.add(pasteMenuItem);
        textArea.setComponentPopupMenu(popupMenu);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                new EditorFrame().setVisible(true);
            }
        });
    }
}